package design.pattern.Lu02FactoryPattern;

/**
 * @program JavaBase
 * @description: 工厂模式
 * @author: zhanglu
 * @create: 2020-03-27 12:22:00
 */
public class Lu02FactoryPattern {

    public static void main(String[] args) {

        Human human = new HumanFactory().createHuman(YellowHuman.class);

        human.getColor();
        human.talk();

    }

}

interface Human{

    void getColor();

    void talk();

}

abstract class AbstractHumanFactory{

    public abstract <T extends Human> T createHuman(Class<T> tClass);

}

class HumanFactory extends AbstractHumanFactory{

    @Override
    public <T extends Human> T createHuman(Class<T> tClass) {
        Human human = null;
        try {
            human = tClass.newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return (T) human;
    }

}

class WhiteHuman implements Human{

    @Override
    public void getColor() {
        System.out.println("白色");
    }

    @Override
    public void talk() {
        System.out.println("英语");
    }

}

class BlackHuma implements Human{

    @Override
    public void getColor() {
        System.out.println("黑色");
    }

    @Override
    public void talk() {
        System.out.println("印度语");
    }
}

class YellowHuman implements Human{

    @Override
    public void getColor() {
        System.out.println("黄色");
    }

    @Override
    public void talk() {
        System.out.println("汉语");
    }
}


